package com.ruoyi.web.controller.house;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.costQuery.domain.HouseCostT;
import com.ruoyi.costQuery.service.IHouseCostQueryTService;
import com.ruoyi.data.DataUtils;
import com.ruoyi.data.MD5;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.service.IHouseDoorTService;
import com.ruoyi.project.domain.HouseProjectT;
import com.ruoyi.purpose.domain.HousePurposeT;
import com.ruoyi.purpose.service.IHousePurposeTService;
import com.ruoyi.repairQuery.domain.RepairQuery;
import com.ruoyi.repairQuery.service.IRepairQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 户管理Controller
 * 
 * @author lijie
 * @date 2023-04-20
 */
@RestController
@RequestMapping("/house/door")
public class HouseDoorTController extends BaseController
{
    @Autowired
    private IHouseDoorTService houseDoorTService;

    @Autowired
    private IHousePurposeTService housePurposeTService;

    @Autowired
    private IHouseCostQueryTService houseCostQueryTService;

    @Autowired
    private IRepairQueryService repairQueryService;
    /**
     * 查询户管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseDoorT houseDoorT)
    {
        startPage();
        List<HouseDoorT> list = houseDoorTService.selectHouseDoorTList(houseDoorT);
        if (list.size()>0) {
            for (HouseDoorT houseDoorVO : list) {
                if (houseDoorVO.getPapersCode() != null) {
                    houseDoorVO.setPapersCode(MD5.md5(houseDoorVO.getPapersCode()));
                }
            }
            if (getDataTableSumJudge(list)) {
                HouseDoorT houseVO = new HouseDoorT();
                houseVO = houseDoorTService.selectHouseDoorSum(houseDoorT);
                if (houseVO != null) {
                    houseVO.setAreaName("合计");
                }
                list.add(houseVO);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出户管理列表
     */
    @Log(title = "户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseDoorT houseDoorT)
    {
        List<HouseDoorT> list = houseDoorTService.selectHouseDoorTList(houseDoorT);
        for(HouseDoorT houseDoorVO:list){
            houseDoorVO.setPapersCode(MD5.md5(houseDoorVO.getPapersCode()));
        }
        if(getDataTableSumJudge(list)){
            HouseDoorT houseVO = houseDoorTService.selectHouseDoorSum(houseDoorT);
            if(houseVO!=null){
                houseVO.setAreaName("合计");
            }
            list.add(houseVO);
        }
        ExcelUtil<HouseDoorT> util = new ExcelUtil<HouseDoorT>(HouseDoorT.class);
        util.exportExcel(response, list, "户管理数据");
    }

    /**
     * 获取户管理详细信息
     */
    @GetMapping(value = "/{doorId}")
    public AjaxResult getInfo(@PathVariable("doorId") Long doorId)
    {
        HouseDoorT houseDoorT = houseDoorTService.selectHouseDoorTByDoorId(doorId);
        if (houseDoorT.getPapersCode()!=null) {
            houseDoorT.setPapersCode(MD5.md5(houseDoorT.getPapersCode()));
        }
        return AjaxResult.success(houseDoorT);
    }

    /**
     * 获取户管理详细信息
     */
    @GetMapping(value = "/getDoorAddr/{doorId}")
    public AjaxResult getDoorAddr(@PathVariable("doorId") Long doorId)
    {
        HouseDoorT houseDoorT = houseDoorTService.selectHouseDoorAddrByDoorId(doorId);
        return AjaxResult.success(houseDoorT);
    }
        /**
     * 新增户管理
     */
    @Log(title = "户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseDoorT houseDoorT)
    {
        int id = houseDoorTService.getDoorId();
        HousePurposeT housePurposeT = new HousePurposeT();
        housePurposeT = housePurposeTService.selectHousePurposeTByPurposeName(houseDoorT.getPurposeName());
        houseDoorT.setBudgetMoney(houseDoorT.getBudgetMoney());
        houseDoorT.setPurposeId(housePurposeT.getPurposeId());
        HouseDoorT vo=new HouseDoorT();
        vo.setAreaName(houseDoorT.getAreaName());
        vo.setDoorName(houseDoorT.getDoorName());
        vo.setGroupName(houseDoorT.getGroupName());
        vo.setUnitName(houseDoorT.getUnitName());
        vo=houseDoorTService.selectDoor(vo);
        if(vo!=null){
            return AjaxResult.error("该户信息已存在");
        }
        String houseId = (DateUtils.dateTimeNow()).substring(2,4) + String.format("%" + 5 + "d", id+1).replace(" ", "0");
        houseDoorT.setCreater(getUsername());
        houseDoorT.setHouseId(houseId);
        houseDoorT.setPapersCode(MD5.md5(houseDoorT.getPapersCode()));
        houseDoorT.setRateType("0");
        return toAjax(houseDoorTService.insertHouseDoorT(houseDoorT));
    }

    /**
     * 修改户管理
     */
    @Log(title = "户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseDoorT houseDoorT)
    {
        HousePurposeT housePurposeT = new HousePurposeT();
        housePurposeT = housePurposeTService.selectHousePurposeTByPurposeId(houseDoorT.getPurposeId());
        Double budgetMoney = 0d;
        if(houseDoorT.getPurposeId()==1){
            budgetMoney = DataUtils.multiplyMoney(housePurposeT.getBuildMoney(),housePurposeT.getPurposeMoney(),houseDoorT.getPrice());
        }else{
            budgetMoney = DataUtils.multiplyMoney(housePurposeT.getBuildMoney(),housePurposeT.getPurposeMoney(),houseDoorT.getSpace());
        }
        houseDoorT.setBudgetMoney(budgetMoney);
        houseDoorT.setPapersCode(MD5.md5(houseDoorT.getPapersCode()));
        return toAjax(houseDoorTService.updateHouseDoorT(houseDoorT));
    }

    /**
     * 删除户管理
     */
    @Log(title = "户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{doorIds}")
    public AjaxResult remove(@PathVariable Long[] doorIds)
    {
        for(Long doorId:doorIds){
            HouseDoorT houseDoorT = new HouseDoorT();
            houseDoorT = houseDoorTService.selectHouseDoorTByDoorId(doorId);
            HouseCostT houseCostT = new HouseCostT();
            houseCostT.setHouseId(houseDoorT.getHouseId());
            List<HouseCostT> costList = houseCostQueryTService.selectHouseCostTList(houseCostT);
            RepairQuery repairQueryVO = new RepairQuery();
            repairQueryVO.setHouseId(houseDoorT.getHouseId());
            List<RepairQuery> list = repairQueryService.selectRepairQueryList(repairQueryVO);
            if(costList.size()>0||list.size()>0){
                return AjaxResult.error("操作失败,该用户已缴过费用或者已经产生维修！");
            }
        }
        return toAjax(houseDoorTService.deleteHouseDoorTByDoorIds(doorIds));
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,String areaName) throws Exception
    {
        ExcelUtil<HouseDoorT> util = new ExcelUtil<HouseDoorT>(HouseDoorT.class);
        List<HouseDoorT> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = houseDoorTService.importUser(userList, updateSupport, operName,areaName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<HouseDoorT> util = new ExcelUtil<HouseDoorT>(HouseDoorT.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 查询计息列表
     */
    @GetMapping("/listRateQuery")
    public TableDataInfo listRateQuery(HouseDoorT houseDoorT)
    {
        startPage();
        List<HouseDoorT> list = houseDoorTService.selectHouseRateTList(houseDoorT);
        return getDataTable(list);
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importAllData")
    public AjaxResult importAllData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<HouseDoorT> util = new ExcelUtil<HouseDoorT>(HouseDoorT.class);
        List<HouseDoorT> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = houseDoorTService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }


    /**
     * 首页信息
     */
    @GetMapping(value = "/getIndexSelectBoxCount01")
    public AjaxResult getIndexSelectBoxCount01 (){
        HouseDoorT vo =new HouseDoorT();
        HouseDoorT vo1 =new HouseDoorT();
        HouseDoorT vo2 =new HouseDoorT();
        HouseDoorT vo3 =new HouseDoorT();
        HouseDoorT vo4 =new HouseDoorT();
        vo1 = houseDoorTService.getBudgetInfo();
        vo2 = houseDoorTService.getCostInfo();
        vo3 = houseDoorTService.getUnpaidInfo();
        vo4 = houseDoorTService.getRepairInfo();
        vo.setBudgetMoney(vo1.getBudgetMoney());
        vo.setDoorSum(vo1.getDoorSum());
        vo.setCostMoney(vo2.getCostMoney());
        vo.setCostMoneySum(vo2.getCostMoneySum());
        vo.setUnpaidMoney(vo3.getUnpaidMoney());
        vo.setUnpaidSum(vo3.getUnpaidSum());
        vo.setRepairMoney(vo4.getRepairMoney());
        vo.setRepairSum(vo4.getRepairSum());
        return AjaxResult.success(vo);
    }



}
